package com.example.test_1;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.List;

public class demoBoxButton extends AppCompatActivity {

    /** 多选框 */
    private CheckBox checkBox,checkBox1,checkBox2,checkBox3;
    private TextView textView;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_demo_box_button);

        checkBox = findViewById(R.id.lanqiu);
        checkBox1 = findViewById(R.id.zuqiu);
        checkBox2 = findViewById(R.id.tiaowu);
        checkBox3 = findViewById(R.id.tiaogao);
        textView  = findViewById(R.id.hhhh);
        button = findViewById(R.id.anniu);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<String> sss = new ArrayList<>();
                if (checkBox.isChecked()){
                    sss.add(checkBox.getText().toString());
                }
                if (checkBox1.isChecked()){
                    sss.add(checkBox1.getText().toString());
                }
                if (checkBox2.isChecked()){
                    sss.add(checkBox2.getText().toString());
                }
                if (sss.isEmpty()){
                    Toast.makeText(demoBoxButton.this, "必须选择一个", Toast.LENGTH_LONG).show();
                    return;
                }
                textView.setText("你的爱好是："+sss.toString());
            }

        });

    }
}